package net.solvetheriddle.wwclient;

import net.solvetheriddle.wwclient.entity.Project;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

@Deprecated
public class WWDBManager {
    private static final String DATABASE_NAME = "wwclient.db";
    private static final String DATABASE_TABLE = "projects";
    private static final int DATABASE_VERSION = 1;

    public static final String KEY_ID = "id";
    public static final int COLUMN_ID = 0;
    public static final String KEY_NAME = "name";
    public static final int COLUMN_NAME = 1;
    public static final String KEY_ACTIVE = "active";
    public static final int COLUMN_ACTIVE = 2;

    private WWClientDBOpenHelper dbHelper;
    private SQLiteDatabase db;
//    private final Context context;

    public WWDBManager(Context context) {
//        this.context = context;
        dbHelper = new WWClientDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void open() {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();       
    }

    public void insertProject(Project project) {
        int activeInt = 0;
        if(project.isActive()) {
            activeInt = 1;
        }
        ContentValues projectValues = new ContentValues();
        projectValues.put(KEY_ID, project.getId());
        projectValues.put(KEY_NAME, project.getName());
        projectValues.put(KEY_ACTIVE, activeInt);
        db.insert(DATABASE_TABLE, null, projectValues);
    }

    public Cursor getAllProjectsCursor() {
        return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_ACTIVE}, null, null, null, null, null);
    }

    public Cursor setCursorToProject(long projectId) throws SQLException {
        Cursor result = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_ACTIVE}, KEY_ID + "=" + projectId, null, null, null, null, null);
        if ((result.getCount() == 0) || !result.moveToFirst()) {
            throw new SQLException("No Projects found for row: " + projectId);
        }
        return result;
    }

    public Project getProject(long rowIndex) throws SQLException {
        Cursor cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_ACTIVE}, KEY_ID + "=" + rowIndex, null, null, null, null, null);
        if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
            throw new SQLException("No Project found for row: " + rowIndex);
        }
        int projectId = cursor.getInt(cursor.getColumnIndex(KEY_ID));
        String name = cursor.getString(cursor.getColumnIndex(KEY_NAME));
        int activeInt = cursor.getInt(cursor.getColumnIndex(KEY_ACTIVE));
        boolean active = false;
        if(activeInt==1) {
            active = true;
        }
        Project result = new Project(projectId, name, active);
        return result;
    }

    private static class WWClientDBOpenHelper extends SQLiteOpenHelper {

        private static final String DATABASE_CREATE = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_NAME
                + " text not null, " + KEY_ACTIVE + " integer not null);";

        public WWClientDBOpenHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
            System.err.println(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // drop the old table
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            // create a new one
            onCreate(db);
        }

    }

}

//open database
// dbManager = new WWDBManager(this);
// dbManager.open();
// dbManager.insertProject(projectList.get(0));
// projectListAdapter.notifyDataSetChanged();
// populateProjectList();
// projectList.clear();

//@Override
// protected void onDestroy() {
// dbManager.close();
// super.onDestroy();
// }

// private void populateProjectList() {
// // get all Projects from the database
// projectCursor = dbManager.getAllProjectsCursor();
// startManagingCursor(projectCursor);
// // update array
// updateArray();
// }

// private void updateArray() {
// projectCursor.requery();
// projectList.clear();
// if (projectCursor.moveToFirst()) {
// do {
// int id = projectCursor.getInt(WWDBManager.COLUMN_ID);
// String name = projectCursor.getString(WWDBManager.COLUMN_NAME);
// int activeInt = projectCursor.getInt(WWDBManager.COLUMN_ACTIVE);
// boolean active = false;
// if (activeInt == 1) {
// active = true;
// }
// Project newProject = new Project(id, name, active);
// } while (projectCursor.moveToNext());
// projectListAdapter.notifyDataSetChanged();
// }
// }


